<?php

namespace app\admin\controller\swoole;

use think\swoole\template\Task;

class MiniCusMsgTask extends Task
{
    public $ghid;
    public $token;
    public $msgid;

    public function initialize($args)
    {
        $this->ghid = $args[0];
        $this->token = $args[1];
        $this->msgid = $args[2];
    }

    public function run($serv, $taskId, $fromWorkerId)
    {
        $file_path = '/www/wwwroot/wechat.nanxuncn.cn/public/static/uploads/20190108/1444171f38277ad61e6d1b81d275fca7.png';
        $file_data = array(
            'media' => new \CURLFile($file_path)
        );

        $opts = array(
            CURLOPT_TIMEOUT => 30,
            CURLOPT_RETURNTRANSFER => 1,
            CURLOPT_SSL_VERIFYPEER => false,
            CURLOPT_SSL_VERIFYHOST => false,
            CURLOPT_POST => 1,
            CURLOPT_URL => 'https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=' . $this->token . '&type=image',
            CURLOPT_POSTFIELDS => $file_data,
        );

        // 初始化并执行curl请求
        $ch = curl_init();
        curl_setopt_array($ch, $opts);
        $data = curl_exec($ch);
        $error = curl_error($ch);
        curl_close($ch);
        if ($error) throw new Exception('请求发生错误; ' . $error);
        var_dump($data);
    }

}
